import os
import random

import pygame


# file_name_and_extension_name = 'E:\work\pycharm\gitee\\test\helloWorld\justPlay\ourSatoshi\satoshi.png'
# file_name, extension_name = os.path.splitext(file_name_and_extension_name)
# print(file_name)
# print(extension_name)


class Game:
    def __init__(self):
        # pygame.init()
        # pygame.display.set_mode(800,600)
        self.screen = pygame.display.get_surface()
        self.clock = pygame.time.Clock()

    def run(self,GRAP):
        # GRAP = get_grahics("E:\work\pycharm\gitee\\test\helloWorld\justPlay\ourSatoshi")
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.display.quit()
                elif event.type == pygame.KEYDOWN:
                    self.keys = pygame.key.get_pressed()
                elif event.type == pygame.KEYUP:
                    self.keys = pygame.key.get_pressed()
            # 没卵用的彩蛋 随机屏幕颜色
            self.screen.fill((random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
            # test
            image = get_image(GRAP['satoshi'], 1, 1, 330, 330, (0, 0, 0), random.randint(1,5))
            self.screen.blit(image, (150, 150))
            pygame.display.update()
            self.clock.tick(20)


def load_grahics(path, accept=('.jpg', '.png')):
    graphics = {}
    for pic in os.listdir(path):
        name, ext = os.path.splitext(pic)
        if ext.lower() in accept:
            img = pygame.image.load(os.path.join(path, pic))
            img = img.convert()
            # 针对透明底图片渲染处理 其实duck不必
            # if img.get_alpha():
            #     img = img.convert_alpha()
            # else:
            #     img = img.convert()
            graphics[name] = img
    return graphics


# 复数素材在一个图片的话 通过底色抠图
def get_image(sheet, x, y, width, height, colorkey, scale):
    # 像素要自己数 有些僵硬 但是凑合吧
    image = pygame.Surface((width, height))
    image.blit(sheet, (0, 0), (x, y, width, height))
    # 抠图key
    image.set_colorkey(colorkey)
    image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
    return image
